package com.situ.supermarket.dao;

import com.situ.supermarket.entity.Area;
import com.situ.supermarket.entity.User;
import com.situ.supermarket.util.DBUtil;
import com.situ.supermarket.util.StringUtil;

import java.util.List;

public class AreaDao {

    public List<Area> all() {
        String sql = "select * from area";
        return  DBUtil.executeDQL(sql,Area.class);
    }

    public void delete(Integer id) {
        String sql = "delete from area where id = ? ";
        DBUtil.executeDML(sql,id);
    }

    public List<Area> select(Integer page, Integer limit, String name) {
        String sql="select * from area";
        if(StringUtil.isNotBlank(name)){
            sql+=" where instr(name,?)>0 limit ?,?";
            return DBUtil.executeDQL(sql, Area.class,name,(page-1)*limit,limit);
        }
        sql+=" limit ?,? ";
        return DBUtil.executeDQL(sql,Area.class,(page-1)*limit,limit);
    }

    public Long count(String name) {
        String sql = " select count(1) from area ";

        if (StringUtil.isNotBlank(name)) {
            sql += "  where instr(name,?)>0";
            return DBUtil.executeCount(sql, name);
        }
        return DBUtil.executeCount(sql);
    }

    public void insert(Area a) {
        String sql = "insert into area(name) values(?)";
        DBUtil.executeDML(sql,a.getName());
    }

    public void update(Area a) {
        String sql = "update area set name=? where id=?";
        DBUtil.executeDML(sql,a.getName(),a.getId());
    }

    public List<Area> selectById(Integer id) {
        String sql = "select * from area where id = ?";
        return DBUtil.executeDQL(sql,Area.class,id);
    }
}
